using System;
using System.Collections.Generic;
using System.Text;

using log4net;
using log4net.Appender;
using log4net.Config;
using log4net.Layout;
using log4net.Repository.Hierarchy;
using usm.SolDev.Common.Config;
using System.Windows.Forms;

[assembly: XmlConfiguratorAttribute(Watch = true)]
namespace usm.SolDev.Common
{
    public static class SolDevLogger
    {
        #region Private fields
        private static ILog logger;
        #endregion        
       
        #region Private properties
        /// <summary>
        ///     Return an instance of logger (create in first time called)
        /// </summary>
        private static ILog Instance
        {
            get
            {
                #region Instantiate logger
                if (logger == null)
                {
                    try
                    {
                        Hierarchy hierarchy = (Hierarchy)LogManager.GetRepository();
                        FileAppender appender = new FileAppender(new PatternLayout("%d [%t] %-5p - %m%n"), String.Format("{0}\\ClientLogs.txt", (new Configuration()).ConfigFolder));
                        hierarchy.Root.AddAppender(appender);
                    }
                    catch (Exception)
                    {

                    }

                    Version version = new Version(Application.ProductVersion);
                    logger = LogManager.GetLogger("ClientLogs");

                    logger.Info(" -----------------------=[ Logger started ]=----------------------- ");
                    logger.Info(String.Format("Application version {0}.{1}-{2} beta", version.Major, version.Minor, version.Build));
                }
                #endregion
                return logger;
            }
        }
        #endregion        
        
        #region Logger message print
        
        /// <summary>
        ///     Print out info log message
        /// </summary>
        /// <param name="format">the format of the message</param>
        /// <param name="list">a list of parameters</param>
        public static void Info(string format, params object[] list)
        {
            Instance.InfoFormat(format, list);
        }

        /// <summary>
        ///     Print out debug log message
        /// </summary>
        /// <param name="format">the format of the message</param>
        /// <param name="list">a list of parameters</param>
        public static void Debug(string format, params object[] list)
        {
            Instance.DebugFormat(format, list);
        }

        /// <summary>
        ///     Print out error log message
        /// </summary>
        /// <param name="format">the format of the message</param>
        /// <param name="list">a list of parameters</param>
        public static void Error(string format, params object[] list)
        {
            Instance.ErrorFormat(format, list);
        }

        /// <summary>
        ///     Print out warnning log message
        /// </summary>
        /// <param name="format">the format of the message</param>
        /// <param name="list">a list of parameters</param>
        public static void Warn(string format, params object[] list)
        {
            Instance.WarnFormat(format, list);
        }
        #endregion    
    }
}
